"""
Case Type   : 共享存储-工具-系统内部工具-pg_resetxlog
Case Name   : 共享存储数据库下，指定共享存储参数参数正确，在备节点执行pg_resetxlog命令
Create At   : 2023/4/25
Owner       : opentestcase019
Description :
    1.停止集群;
    2.在对应目录下查看原有xlog列表;
    3.在备节点执行pg_resetxlog命令,指定共享存储参数参数正确;
    4.再次查看原有xlog列表，是否重置成功;
    5.启动数据库;
Expect      :
    1.成功
    2.查看xlog列表成功
    3.执行失败
    4.日志重置失败
    5.启动成功
History     :
"""
import os
import time
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.standby_node = Node('Standby1DbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.node_num = self.primary_sh.get_node_num()

    def test_sharestorage_tool(self):
        step1 = "-----step1:停止集群;expect:成功-----"
        self.logger.info(step1)
        stop_res = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_res)
        self.assertIn(self.constant.cm_stop_success_msg, stop_res,
                      '执行失败' + step1)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)

        step2 = "-----step2:拉起备机dssserver;expect:成功-----"
        self.logger.info(step2)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'dssserver -D {macro.DSS_INSTANCE} -M & '
        self.logger.info(cmd)
        session1 = ComThread(self.common.get_sh_result,
                             args=(self.standby_node, cmd))
        session1.setDaemon(True)
        session1.start()
        time.sleep(2)

        step3 = "-----step3:在备节点执行pg_resetxlog命令,指定共享存储参数参数正确; " \
                "expect:执行失败-----"
        self.logger.info(step3)
        execute_cmd = f"ps ux | grep dssserver;" \
                      f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd ls -p +data/pg_xlog0/ -U {macro.UDS_PATH};" \
                      f"rm -f {macro.DB_INSTANCE_PATH}/postmaster.pid;" \
                      f"pg_resetxlog {macro.DB_INSTANCE_PATH} --enable-dss " \
                      f"--socketpath={macro.UDS_PATH} --vgname=+data "
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('pg_resetxlog: you must execute this command in '
                      'primary node: 0, current node id is', execute_msg,
                      "执行失败" + step3)
        self.assertNotIn('Transaction log reset', execute_msg,
                         "执行失败" + step3)

        step4 = "-----step4:kill dssserver 进程;expect:成功-----"
        self.logger.info(step4)
        execute_cmd = f"ps ux | grep dssserver | grep -v grep | " \
                      f"awk '{{{{print $2}}}}' | xargs kill -9"
        self.logger.info(execute_cmd)
        self.common.get_sh_result(self.standby_node, execute_cmd)
        session1.join(30)
        result1 = session1.get_result()
        self.logger.info(f'session1_results: {result1}')
        self.assertEqual('', result1, "执行失败" + step4)

    def tearDown(self):
        step5 = "-----step5:启动数据库;expect:启动成功-----"
        self.logger.info(step5)
        start_res = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_res)
        self.assertIn(self.constant.cm_start_success_msg, start_res,
                      '执行失败' + step5)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Civdp')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step5)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
